package game.editer;

import game.editer.view.AtlasOffestEditerView;
import game.editer.view.AlertView;
import game.editer.view.CreateEffectPreviewView;
import game.editer.ui.CommonButton;
import hx.display.HBox;
import hx.display.Quad;
import hx.display.VScrollBar;
import game.data.FrameData;
import hx.events.MouseEvent;
import hx.layout.FlowLayout;
import hx.display.Scroll;
import hx.display.Image;
import game.assets.HxwzAssets;
import hx.events.Event;
import hx.display.ArrayCollection;
import game.data.IRoleData;
import hx.layout.AnchorLayoutData;
import game.editer.ui.DownDropListView;

/**
 * 精灵图库编辑器
 */
class SpritePoolEditer extends BasePanel {
	/**
	 * 项目数据
	 */
	public var projectData(default, set):IRoleData;

	public var atlasListView:DownDropListView;

	private function set_projectData(value:IRoleData):IRoleData {
		projectData = value;
		updateProjectData();
		return value;
	}

	public function new() {
		super("");
	}

	public var scrollBox:Scroll;

	override function onInit() {
		super.onInit();
		//
		var hBox = new HBox();
		hBox.gap = 5;
		this.addChild(hBox);
		hBox.layoutData = AnchorLayoutData.topLeft(3, 4);
		atlasListView = new DownDropListView();
		hBox.addChild(atlasListView);
		hBox.verticalAlign = MIDDLE;
		atlasListView.addEventListener(Event.CHANGE, onAtlasChange);

		var editBtn = new CommonButton("UIAtlas:edit");
		hBox.addChild(editBtn);
		editBtn.addEventListener(MouseEvent.CLICK, (e:MouseEvent) -> {
			var view = new AtlasOffestEditerView("精灵图属性编辑", 800, 700);
			view.edit(this.atlasListView.selectedItem.text);
			this.topView.addChild(view);
		});

		var addAtlas = new CommonButton("UIAtlas:add");
		hBox.addChild(addAtlas);
		addAtlas.addEventListener(MouseEvent.CLICK, (e:MouseEvent) -> {
			var view = new CreateEffectPreviewView("添加精灵图", 800, 600);
			this.topView.addChild(view);
			view.onCreateEffect = (effect) -> {
				if (EditerStage.roleAssets != null) {
					addSprite(effect.name);
				}
			}
		});

		var removeAtlas = new CommonButton("UIAtlas:trash");
		hBox.addChild(removeAtlas);
		removeAtlas.addEventListener(MouseEvent.CLICK, (e:MouseEvent) -> {
			if (EditerStage.roleAssets != null) {
				AlertView.showView("提示", "是否删除【" + this.atlasListView.selectedItem.text + "】精灵图？", ["取消", "确定"], (tag) -> {
					if (tag == "确定") {
						this.removeAtlas(this.atlasListView.selectedItem.text);
					}
				});
			}
		});

		scrollBox = new Scroll();
		this.addChild(scrollBox);
		scrollBox.layoutData = new AnchorLayoutData(36, 2, 2, 2);
		scrollBox.layout = new FlowLayout(10, 10);
		scrollBox.scrollXEnable = false;
		scrollBox.autoVisible = true;
		scrollBox.addEventListener(MouseEvent.CLICK, onSpriteClicked);
		var bar = new VScrollBar(0x49494a, 0x1d1d1d);
		this.addChild(bar);
		bar.layoutData = new AnchorLayoutData(36, 0, 1, null);
		bar.width = 6;
		scrollBox.vScrollBar = bar;
	}

	public function addSprite(id:String):Void {
		if (projectData.atlas.indexOf(id) == -1) {
			projectData.atlas.push(id);
			this.updateProjectData();
		} else {
			AlertView.showView("提示", "精灵图已存在");
		}
	}

	public function removeAtlas(id:String):Void {
		if (projectData.atlas.remove(id)) {
			this.updateProjectData();
		} else {
			AlertView.showView("提示", "精灵图不存在");
		}
	}

	private function onSpriteClicked(e:MouseEvent):Void {
		var sprite:SpriteFrame = e.target;
		if (sprite is SpriteFrame) {
			// 插入帧
			var frameData = new FrameData();
			frameData.name = sprite.data.name;
			EditerStage.timeline.insertFrame(frameData);
		}
	}

	private function onAtlasChange(e:Event):Void {
		var data = atlasListView.selectedItem;
		if (data == null)
			return;
		var assets = EditerStage.assets;
		var atlas = EditerStage.world.role.assets.getAtlas(data.text);
		scrollBox.removeChildren();
		for (index => key in atlas.names) {
			var image = new SpriteFrame({
				index: index,
				name: data.text + ":" + key
			}, atlas.bitmapDatas.get(key));
			scrollBox.addChild(image);
		}
		scrollBox.updateLayout();
	}

	/**
	 * 更新项目数据
	 */
	public function updateProjectData():Void {
		if (projectData == null)
			return;
		atlasListView.data = new ArrayCollection([
			for (key => value in projectData.atlas) {
				{
					key: key,
					text: value
				}
			}
		]);
		atlasListView.selectedIndex = 0;
		onAtlasChange(null);
	}
}
